package plugins.seat;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.bill.BillShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.StyleCss;
import kd.bos.form.control.Control;
import java.util.EventObject;
import kd.sdk.plugin.Plugin;

import static com.bes.appserv.lic.g.i;

/**
 * 动态表单插件
 */
public class SeatApply extends AbstractFormPlugin implements Plugin {
    // 定义标识
    private final String UNAVAIL_BLACK_SEAT = "dcb4_unavail_black_";
    private final String UNAVAIL_RED_SEAT = "dcb4_unavail_red_";
    private final String AVAIL_BLACK_SEAT = "dcb4_avail_black_";
    private final String AVAIL_RED_SEAT = "dcb4_avail_red_";
    private final String SEAT_APPLY = "dcb4_seat_apply";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        // 不可预约区
        for (int row = 1; row <= 25; row++) {
            for (int col = 1; col <= 25; col++) {
                this.addClickListeners(UNAVAIL_BLACK_SEAT + "r" + row + "_c" + col);
                this.addClickListeners(UNAVAIL_RED_SEAT + "r" + row + "_c" + col);
            }
        }

        // 可预约区
        for (int row = 1; row <= 25; row++) {
            for (int col = 1; col <= 25; col++) {
                this.addClickListeners(AVAIL_BLACK_SEAT + "r" + row + "_c" + col);
                this.addClickListeners(AVAIL_RED_SEAT + "r" + row + "_c" + col);
            }
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);

        // 不可预约区全部设置为黑色初始显示
        for (int row = 1; row <= 25; row++) {
            for (int col = 1; col <= 25; col++) {
                this.getView().setVisible(true, UNAVAIL_BLACK_SEAT + "r" + row + "_c" + col);
                this.getView().setVisible(false, UNAVAIL_RED_SEAT + "r" + row + "_c" + col);
            }
        }

        // 可预约区需要根据是否预定进行显示
        for (int row = 1; row <= 25; row++) {
            for (int col = 1; col <= 25; col++) {
                // 这里的 S00000 是座位号的前缀，根据你在座位基础资料里的编码设置来修改
                String prefix = "S";
                String rowPart = String.format("%02d", row); // 行号，两位数
                String colPart = String.format("%02d", col); // 列号，假设每行只有一个座位，可以根据实际情况调整
                String seatNo = prefix + rowPart + colPart;

                QFilter qFilter = new QFilter("number", QCP.equals, seatNo);
                DynamicObject goalSeat = BusinessDataServiceHelper.loadSingle(
                        "dcb4_seatdata",
                        new QFilter[]{qFilter}
                );

                boolean isBooked = goalSeat != null && goalSeat.getString("dcb4_combofield") != null;
                if (isBooked) {
                    this.getView().setVisible(false, AVAIL_BLACK_SEAT + "r" + row + "_c" + col);
                    this.getView().setVisible(true, AVAIL_RED_SEAT + "r" + row + "_c" + col);
                } else {
                    this.getView().setVisible(true, AVAIL_BLACK_SEAT + "r" + row + "_c" + col);
                    this.getView().setVisible(false, AVAIL_RED_SEAT + "r" + row + "_c" + col);
                }
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();

        if (source != null) {
            String key = source.getKey();

            if (key.startsWith(UNAVAIL_BLACK_SEAT) || key.startsWith(UNAVAIL_RED_SEAT)) {
                // 不可预约区，提示不可预约
                this.getView().showMessage("该区域不可预约！");
            } else if (key.startsWith(AVAIL_BLACK_SEAT)) {
                // 可预约区未预定 → 弹出预约窗口
                String seatPos = key.substring(AVAIL_BLACK_SEAT.length());
                // seatPos like r2_c3
                String[] pos = seatPos.split("_");
                int row = Integer.parseInt(pos[0].substring(1)); // 去掉 r
                int col = Integer.parseInt(pos[1].substring(1)); // 去掉 c

                String prefix = "S";
                String rowPart = String.format("%02d", row); // 行号，两位数
                String colPart = String.format("%02d", col); // 列号，假设每行只有一个座位，可以根据实际情况调整
                String seatNo = prefix + rowPart + colPart;

                BillShowParameter parameter = new BillShowParameter();
                parameter.setFormId(SEAT_APPLY);
                parameter.getOpenStyle().setShowType(ShowType.Modal);
                parameter.setCustomParam("dcb4_basedatafield_seat", seatNo);

                StyleCss styleCss = new StyleCss();
                styleCss.setHeight("600");
                styleCss.setWidth("900");
                parameter.getOpenStyle().setInlineStyleCss(styleCss);

                this.getView().showForm(parameter);

                // 修改颜色：黑色→红色
                this.getView().setVisible(false, AVAIL_BLACK_SEAT + seatPos);
                this.getView().setVisible(true, AVAIL_RED_SEAT + seatPos);

            } else if (key.startsWith(AVAIL_RED_SEAT)) {
                // 可预约区已预定 → 提示
                this.getView().showMessage("该座位已被预定，请选择其他座位");
            }
        }
    }
}
